package com.yegrina.member.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.yegrina.member.dao.MemberDao;
import com.yegrina.member.domain.Member;
import com.yegrina.member.validator.MemberValidator;


@Controller
public class joinController{

	private MemberDao memberDao;
	private String formViewName = "join";
	@Autowired
	public void setMemberDao(MemberDao memberDao) {
		this.memberDao = memberDao;
	}

	@RequestMapping(value="/membership/join.do", method=RequestMethod.GET)
	public String form(){
		return formViewName;
	}

	@ModelAttribute
	public Member formBacking() {
		return new Member();
	}

	@RequestMapping(value="/membership/join.do", method=RequestMethod.POST)
	public String submit(Member memberCommand, BindingResult result){
		new MemberValidator().validate(memberCommand, result);
		
		//DatePicker 데이터 형전환 (Timepicker의 전달값 01/01/2013-> 오라클 표기(2013/01/01)
		String[] birthdays=memberCommand.getBirthday().substring(0,10).split("/");	
		memberCommand.setBirthday(birthdays[2]+"/"+birthdays[0]+"/"+birthdays[1]);
		
		if(result.hasErrors()){
			return formViewName;
		}
		
		try{
			memberDao.insertMember(memberCommand);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "join";
	}
}
